﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
using HZ.DrugTrade.Web;
using HZ.Common;

namespace HZ.DrugTrade.Web.Hospitals.Collection
{
    public partial class InvoiceDetail : BasePage
    {
        public string str_InvoiceSerialID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            IBLL_Hospital ih = Factory.BLL_Hospital();
            string hID = CurrentAccount.Account.Org.OrgID;
            Model.HospitalInfo hospital = ih.GetHospitalInfoByID(hID);
            if (hospital != null)
            {
                if (!HZ.DrugTrade.Web.Function.IsThreeLevel())//项目查询条件仅三级医院可见
                {
                    this.ddlProjectid.Visible = false;
                    this.lblProjectid.Visible = false;
                }
            }
            //获取发票ID
            if (Request.QueryString["InvoiceSerialID"] != null)
            {
                str_InvoiceSerialID = Request.QueryString["InvoiceSerialID"].ToString();

            }
            else { return; }

            if (!Page.IsPostBack)
            {
                QueryResult();
                ShowInvocieInfo();
            }
        }
        /// <summary>
        /// 数据绑定,获取发票的详细信息
        /// </summary>
        private void QueryResult()
        {
            IList<Model.View.InvoiceDistribution> lstInvoicePay = new List<Model.View.InvoiceDistribution>();
            Model.Query.InvoiceDistribution qryCondition = new Model.Query.InvoiceDistribution();
            User user = CurrentAccount.GetUser();
            if (user.UserID.Substring(0, 1).ToUpper().Equals("Z"))
            {
                if (Request.QueryString["Hospitalid"] != null)
                {
                    qryCondition.HospitalID = Request.QueryString["Hospitalid"];
                }
            }
            else
            {
                qryCondition.HospitalID = user.Org.OrgID;
            }
            qryCondition.InvoiceSerialID = str_InvoiceSerialID;
            qryCondition.OrderName = txtOrderName.Text.ToString().Trim();
            qryCondition.GoodsName = txtGoodsName.Text.ToString().Trim();
            qryCondition.Material = txtMaterial.Text.ToString().Trim();
            qryCondition.PinyinCode = this.txtpinyincode.Text.Trim();
            qryCondition.ProjectID = this.ddlProjectid.SelectedValue;
            qryCondition.Outlookc = txtOutLookc.Text.ToString().Trim();
            qryCondition.DistributeTimeFrom = new HZ.Common.ConvertHelper(txtTimeFrom.Text.ToString().Trim()).ToDateTime;
            qryCondition.DistributeTimeTo = new HZ.Common.ConvertHelper(txtTimeTo.Text.ToString().Trim()).ToDateTime;
            qryCondition.SortFields = " InvoiceTime desc ";

            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalCount = 0;
            IBLL_Purchase iPurchase = Factory.BLL_Purchase();
            lstInvoicePay = iPurchase.GetInvoiceDistributionByWhere(qryCondition, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwRefundDetail.DataSource = lstInvoicePay;
            gvwRefundDetail.DataBind();

        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;     //update by lhl 2009-08-30
            QueryResult();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            QueryResult();
        }


        /// <summary>
        /// 显示发票信息
        /// </summary> 
        private void ShowInvocieInfo()                          //add by lhl 2009-08-30
        {
            IBLL_Purchase iPurchase = Factory.BLL_Purchase();
            Model.Query.Invoice qryCondition = new Model.Query.Invoice();
            qryCondition.InvoiceSerialID = str_InvoiceSerialID;
            int totalCount = 0;
            Model.View.Invoice invoice = iPurchase.GetInvoiceByWhere(qryCondition, 1, 0, out totalCount)[0];
            if (invoice != null)
            {
                txtInvoiceId.Text = invoice.InvoiceID;
                txtAmount.Text = invoice.TotalAmount.ToString();

                string status = invoice.AuditStatus.ToString();
                if (status == "0")
                {
                    txtStatus.Text = "未提交";
                }
                else if (status == "1")
                {
                    txtStatus.Text = "已提交";
                }
                string companyId = invoice.CompanyID_PS;
                IBLL_Company ic = Factory.BLL_Company();
                txtCompany.Text = ic.GetCompanyInfoByID(companyId).CompanyName;
            }
            else
            {
                PageError("给定发票不存在！", this.ReturnUrl);
            }

        }
    }
}
